package customer.zspt.cn.customer.components.tag;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.bean.SkillListDao;
import customer.zspt.cn.customer.utils.SPUtils;
import customer.zspt.cn.customer.Widget.ContactItemView;

/**
 * Created by Administrator on 2018/7/8 0008.标签
 */

public class tag extends Activity {
    private List<SkillListDao.SkillList> data;
    @ViewInject(R.id.tag_lv)
    private ListView tag_lv;
    @ViewInject(R.id.title)
    private View title;
    @ViewInject(R.id.UserName)
    private TextView UserName;
    @ViewInject(R.id.back)
    private ImageView back;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tag);
        ViewUtils.inject(this);
        UserName.setText("标签");
        View view = findViewById(R.id.empty_layout);
        tag_lv.setEmptyView(view);
        pushData();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    //解析
    private void pushData() {
        String skillList = SPUtils.getString(this, constant.SKILL, "");
        Log.e("tag",skillList);
        try{
            if (!TextUtils.isEmpty(skillList)) {
                Gson gson = new Gson();
                SkillListDao skillListDao = gson.fromJson(skillList, SkillListDao.class);
                data = skillListDao.data;
                tagAdapter adapter = new tagAdapter();
                tag_lv.setAdapter(adapter);
                setClickLister();
            }
        }
        catch (Exception e)
        {
            Toast.makeText(this,"数据异常,可能是由于网络连接出现问题",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

    }
    private void setClickLister() {
        tag_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent item = new Intent(getApplicationContext(), tag_view.class);
                item.putExtra("skillId", data.get(i).skillId);
                startActivity(item);
            }
        });
    }

    class tagAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ContactItemView tag_item = new ContactItemView(getApplicationContext());
            tag_item.icon.setVisibility(View.GONE);
            tag_item.setText(data.get(i).skillTitle);
            return tag_item;
        }
    }
}
